<?php
/*
 * filename: my_item.php
 * author: xuguoyu
 * date: 2010/4/22
 * update:
 * description: 
 */

require_once 'cls/base.php';
require_once 'cls/cls.php';

$tpl = new Template('t');
$tpl->setFile ( "main", "main.html" );
$tpl->setFile("list", "my_item.html");
$tpl->setBlock ( "list", "block1", "block1s" );

// 获取登录用户id
//$uid = intval($my->api_client->user_getLoggedInUser());
$uid=0;

if(pm() == "POST"){
	$ac = fpost("ac");//获取提交类型
	//获取提交数据
	$id = getNum(fpost("id"));
	$caption = addslashes_or_not(fpost("caption"));
	$price = getNum(addslashes_or_not(fpost("price")));
	$acount = getNum(addslashes_or_not(fpost("acount")));
	$appraise = addslashes_or_not(fpost("appraise"));
	$time = addslashes_or_not(fpost("time"));
	$area = addslashes_or_not(fpost("area"));
	$shop = addslashes_or_not(fpost("shop"));
	$explain = addslashes_or_not(fpost("explain"));
	$types = getNum(fpost("types"));

	$opt = new Operate();
	if($ac == "edit"){ //修改
		$item_info = array(
			"id" => $id,
			"caption" => $caption,
			"price" => $price,
			"amount" => $acount,
			"appraise" => $appraise,
			"time" => $time,
			"area" => $area,
			"shop_name" => $shop,
			"explain" => $explain,
			"type" => $types,
			"uid" => $uid
		);
		$msg = $opt->edit_item($item_info) ? "修改成功！" : "修改失败！";
		$tpl->setVar("msg", $msg);
	}
	if($ac == "add"){//添加
		$item_info = array(
			"id" => $id,
			"caption" => $caption,
			"price" => $price,
			"amount" => $acount,
			"appraise" => $appraise,
			"time" => $time,
			"area" => $area,
			"shop_name" => $shop,
			"explain" => $explain,
			"type" => $types,
			"uid" => $uid
		);
		$msg = $opt->add_item($item_info) ? "添加成功！" : "添加失败！";
		$tpl->setVar("msg", $msg);
	}
}

$ac = fget("ac");
if($ac == "del"){ //删除条目
	$id = fget("id");
	$opt = new Operate();
	$msg = $opt->delete_item($id) ? "删除成功！" : "删除失败！";
	$tpl->setVar("msg", $msg);
}

$where = "where user={$uid} "; //查询条件

$db = conn();
$sql = "select count(*) from item " . $where;
$rs=$db->query($sql, MYSQL_NUM);
$page_str = pageft($rs[0][0],10);
$sql = "select * from item " . $where . "order by id desc {$page_str['limit']}";
$rs=$db->query($sql, MYSQL_ASSOC);
if(!empty($rs))
	foreach($rs as $v){
		$tpl->setvar(array("id" => $v['id'],
						"time" => $v['time'],
						"caption" => $v['caption'],
						"price" => $v['price']
				));
		$tpl->parse ( "block1s", "block1", true );
	}
$tpl->setvar("page_str", $page_str["str"]);

$db->close();

$tpl->parse('main_content', 'list');
$tpl->p("main");
?>